package fivechess1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FiveChess extends JFrame {
    ChessGame CG;

    FiveChess() {
        this.CG = new ChessGame();
        addMouseListener(CG);
        add(CG);
    }

    public static void main(String[] args) {
        FiveChess fiveChess = new FiveChess();
        fiveChess.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fiveChess.setSize(900, 700);
        fiveChess.setTitle("五子棋");
        fiveChess.setVisible(true);
        fiveChess.setResizable(true);
    }
}

class ChessGame extends JPanel implements MouseListener, ItemListener {
    int[][] map = new int[20][20];
    Color[] color = {Color.BLACK, Color.WHITE, Color.LIGHT_GRAY}; // 棋子颜色
    int x = -1, y = -1;
    int flag = 1; // 判定是白棋还是黑棋
    int winner = 0; // 判定胜负
    JButton btn1 = new JButton("开始"); // 游戏开始
    JButton btn2 = new JButton("再来一局"); // 游戏重置
    CheckboxGroup cbg = new CheckboxGroup();
    Checkbox chb1 = new Checkbox("黑棋先开始", cbg, true);
    Checkbox chb2 = new Checkbox("白棋先开始", cbg, false);

    @Override
    public void itemStateChanged(ItemEvent ie) {
        if (chb1.getState()) {
            flag = 1;
        } else {
            flag = 2;
        }
    }

    class GameStart implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() == btn1) {
                start();
                repaint();
                btn1.setEnabled(false);
                btn2.setEnabled(true);
                chb1.setEnabled(false);
                chb2.setEnabled(false);
            }
            if (ae.getSource() == btn2) {
                start();
                repaint();
                btn1.setEnabled(true);
                btn2.setEnabled(false);
                chb1.setEnabled(true);
                chb2.setEnabled(true);
            }
        }
    }

    ChessGame() {
        this.setLayout(null);
        chb1.setBounds(650, 50, 100, 30);
        chb2.setBounds(650, 100, 100, 30);
        chb1.addItemListener(this);
        chb2.addItemListener(this);
        btn1.setBounds(650, 150, 100, 30);
        btn1.addActionListener(new GameStart());
        btn2.setBounds(650, 200, 100, 30);
        btn2.addActionListener(new GameStart());
        btn2.setEnabled(false);
        this.add(btn1);
        this.add(btn2);
        this.add(chb1);
        this.add(chb2);
    }

    void start() {
        for (int i = 0; i < 20; i++)
            for (int j = 0; j < 20; j++)
                map[i][j] = 0;
        winner = 0;
    }

    boolean win(int x, int y) {
        int score;

        // 横向判断
        score = 1;
        for (int i = x; i < x + 4 && i < 19; i++) {
            if (map[i][y] != map[i + 1][y])
                break;
            score++;
        }
        for (int i = x; i > x - 4 && i > 0; i--) {
            if (map[i][y] != map[i - 1][y])
                break;
            score++;
        }
        if (score >= 5)
            return true;

        // 纵向判断
        score = 1;
        for (int j = y; j < y + 4 && j < 19; j++) {
            if (map[x][j] != map[x][j + 1])
                break;
            score++;
        }
        for (int j = y; j > y - 4 && j > 0; j--) {
            if (map[x][j] != map[x][j - 1])
                break;
            score++;
        }
        if (score >= 5)
            return true;

        // 斜向判断（从左上到右下）
        score = 1;
        for (int i = x, j = y; i < x + 4 && j < y + 4 && i < 19 && j < 19; i++, j++) {
            if (map[i][j] != map[i + 1][j + 1])
                break;
            score++;
        }
        for (int i = x, j = y; i > x - 4 && j > y - 4 && i > 0 && j > 0; i--, j--) {
            if (map[i][j] != map[i - 1][j - 1])
                break;
            score++;
        }
        if (score >= 5)
            return true;

        // 斜向判断（从右上到左下）
        score = 1;
        for (int i = x, j = y; i > x - 4 && j < y + 4 && i > 0 && j < 19; i--, j++) {
            if (map[i][j] != map[i - 1][j + 1])
                break;
            score++;
        }
        for (int i = x, j = y; i < x + 4 && j > y - 4 && i < 19 && j > 0; i++, j--) {
            if (map[i][j] != map[i + 1][j - 1])
                break;
            score++;
        }
        return score >= 5;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(color[2]);
        // 画棋盘
        for (int i = 1; i <= 20; i++) {
            g.setColor(color[0]);
            g.drawLine(30, i * 30, 600, i * 30);
            g.drawLine(i * 30, 30, i * 30, 600);
        }
        // 画棋子
        for (int i = 0; i < 20; i++)
            for (int j = 0; j < 20; j++) {
                if (map[i][j] == 1) {
                    g.setColor(color[0]);
                    g.fillOval((i + 1) * 30 - 15, (j + 1) * 30 - 15, 30, 30);
                }
                if (map[i][j] == 2) {
                    g.setColor(color[1]);
                    g.fillOval((i + 1) * 30 - 15, (j + 1) * 30 - 15, 30, 30);
                }
            }
        // 判定胜负
        Font font = new Font("", Font.BOLD, 30);
        g.setFont(font);
        if (winner == 1) {
            g.setColor(color[0]);
            g.drawString("黑棋胜", 650, 300);
        }
        if (winner == 2) {
            g.setColor(color[1]);
            g.drawString("白棋胜", 650, 300);
        }
    }

    @Override
    public void mouseClicked(MouseEvent me) {
    }

    @Override
    public void mousePressed(MouseEvent me) {
        if (winner == 0) {
            x = me.getX();
            y = me.getY();
            chb1.setEnabled(false);
            chb2.setEnabled(false);
        }
        x = (x / 30) - 1;
        y = (y / 30) - 1;
        if (x >= 0 && x <= 19 && y >= 0 && y <= 19) {
            if (flag == 1 && map[x][y] == 0) {
                map[x][y] = 1;
                if (win(x, y))
                    winner = 1;
                flag = 2;
            } else if (flag == 2 && map[x][y] == 0) {
                map[x][y] = 2;
                if (win(x, y))
                    winner = 2;
                flag = 1;
            }
        }
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }
}


